/*
 * Copyright 2002-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.spring.sample.graphql.greeting;

import com.querydsl.core.types.Predicate;
import graphql.GraphQLContext;
import graphql.relay.Connection;
import graphql.relay.SimpleListConnection;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import io.spring.sample.graphql.project.Project;
import io.spring.sample.graphql.project.Release;
import io.spring.sample.graphql.project.SpringProjectsClient;
import io.spring.sample.graphql.repository.Book;
import io.spring.sample.graphql.repository.Books;
//import io.spring.sample.graphql.repository.QBook;
import org.dataloader.DataLoader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.data.web.ProjectedPayload;
import org.springframework.graphql.data.method.annotation.*;
import org.springframework.graphql.data.query.QuerydslDataFetcher;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//import static io.spring.sample.graphql.repository.QBook.book;

@Controller
public class BookController {

	private final SpringProjectsClient client;
	private final Books books;

	public BookController(SpringProjectsClient client, Books books) {
		this.client = client;
		this.books = books;
	}

	@QueryMapping
	public Project project2(@Argument String slug) {
		return client.fetchProject(slug);
	}

	//@SchemaMapping
	// Page<Book> books(@QuerydslPredicate(root = Book.class) Predicate predicate)
	//				Pageable pageable , @Argument Map<String, Object> parameters
	/* (@Arguments("bookInput")  BookInputProjection bookInput)

{
    books(name:"做法是",id:"fff") {
        id,name,url  snapshotsEnabled
    }
}
DataLoader<String, Book> loader;	 @ContextValue Predicate predicate,
	* */
	@QueryMapping
	public Connection<Book> books(GraphQLContext ctx, DataFetchingEnvironment dfe) throws Exception {

		Pageable pageable= PageRequest.of(0, 2);
		Sort sort=null;
	  	//Predicate predicate=null;
	//	 predicate = QBook.book.id.equalsIgnoreCase(bookInput.getId())
	//			.and(QBook.book.name.startsWithIgnoreCase(bookInput.getName()));
		//books.findAll(predicate, pageable);
		DataFetcher<Iterable<Book>> dataFetcher =
				QuerydslDataFetcher.builder(books).many();		//可加.sortBy(sort)
		SimpleListConnection<Book> dataFetcher2 =new SimpleListConnection((List) dataFetcher.get(dfe));
		return dataFetcher2.get(dfe);
		//return books.findAll(pageable);
	}

/* 可以成功的方式
{
    books(bookInput:{name:"做法是",id:"fff"}) {
        id,name,url  snapshotsEnabled
    }
}
* */
/*	@QueryMapping
	public Page<Book> books2(@Argument BookInputProjection bookInput) {

		Pageable pageable= PageRequest.of(0, 11);

		Predicate predicate=null;
		predicate = QBook.book.id.equalsIgnoreCase(bookInput.getId())
				.and(QBook.book.name.startsWithIgnoreCase(bookInput.getName()));

		return books.findAll(predicate, pageable);
	}*/
	@QueryMapping
	public Page<Book> books7(DataFetchingEnvironment dfe,@Argument("first") int first) {

		Pageable pageable= PageRequest.of(0, first);

		Predicate predicate=null;
		//	 predicate = QBook.book.id.equalsIgnoreCase(bookInput.getId())
		//			.and(QBook.book.name.startsWithIgnoreCase(bookInput.getName()));
		//books.findAll(predicate, pageable);

		return books.findAll(pageable);
	}
	/*
{
    books(name:"仅仅") {
          id,name,url  snapshotsEnabled
    }
}

	* */
	@QueryMapping
	public Page<Book> books13(GraphQLContext ctx, DataFetchingEnvironment dfe) throws Exception {

		Pageable pageable= PageRequest.of(0, 2);
		Sort sort=null;
		//Predicate predicate=null;
		//	 predicate = QBook.book.id.equalsIgnoreCase(bookInput.getId())
		//			.and(QBook.book.name.startsWithIgnoreCase(bookInput.getName()));
		//books.findAll(predicate, pageable);
		DataFetcher<Iterable<Book>> dataFetcher =
				QuerydslDataFetcher.builder(books).many();		//可加.sortBy(sort)
		SimpleListConnection<Book> dataFetcher2 =new SimpleListConnection((List) dataFetcher.get(dfe));
		return (Page<Book>) dataFetcher2.get(dfe);
		//return books.findAll(pageable);
	}
}

@ProjectedPayload
interface BookInputProjection {
	String getId();
	String getName();

	@Value("#{target.id + ' ^ ' + target.name}")
	String getAuthorAndName();
}
